package com.ruowei.modules.member.web.rest;

import com.fasterxml.jackson.annotation.JsonView;
import com.ruowei.common.error.exception.BadRequestAlertException;
import com.ruowei.modules.member.domain.MemberReceivingInfo;
import com.ruowei.modules.member.repository.MemberReceivingInfoRepository;
import com.ruowei.modules.member.web.dto.ReceiveInfoQM;
import com.ruowei.modules.member.web.dto.UpdateReceivingQM;
import com.ruowei.modules.sys.utils.AppletTokenUtils;
import com.ruowei.modules.sys.utils.CommonUtils;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.ruowei.modules.member.domain.MemberReceivingInfo}.
 */
@RestController
@RequestMapping("/api")
@Transactional
@Api(tags = "我的")
public class MemberReceivingInfoResource {

    private final Logger log = LoggerFactory.getLogger(MemberReceivingInfoResource.class);

    private final MemberReceivingInfoRepository memberReceivingInfoRepository;

    public MemberReceivingInfoResource(MemberReceivingInfoRepository memberReceivingInfoRepository) {
        this.memberReceivingInfoRepository = memberReceivingInfoRepository;
    }

    @PostMapping("/applet/mine/addReceivingInfo")
    @ApiOperation(value = "1-新增收货信息")
    public ResponseEntity<Void> insertReceiveInfo(HttpServletRequest request, @RequestBody ReceiveInfoQM qm) {
        if (StringUtils.isEmpty(qm.getName())) {
            throw new BadRequestAlertException("请填写用户姓名");
        }
        if (StringUtils.isEmpty(qm.getPhone())) {
            throw new BadRequestAlertException("请填写用户电话");
        }
        MemberReceivingInfo receivingInfo = new MemberReceivingInfo();
        receivingInfo.setInfoCode(CommonUtils.getUUID());
        receivingInfo.setMemberCode(AppletTokenUtils.getOpenIdByToken(request).getOpenId());
        receivingInfo.setPhone(qm.getPhone());
        receivingInfo.setName(qm.getName());
        memberReceivingInfoRepository.save(receivingInfo);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/applet/mine/getReceivingInfo")
    @ApiOperation(value = "2-获取用户收货信息列表")
    public ResponseEntity<List<MemberReceivingInfo>> getReceivingInfo(HttpServletRequest request) {
        List<MemberReceivingInfo> infos = memberReceivingInfoRepository.findAllByMemberCode(AppletTokenUtils.getOpenIdByToken(request).getOpenId());
        return ResponseEntity.ok(infos);
    }

    @PutMapping("/applet/mine/updateReceivingInfo")
    @ApiOperation(value = "3-修改用户信息")
    public ResponseEntity<MemberReceivingInfo> updateReceivingInfo(HttpServletRequest request, @RequestBody UpdateReceivingQM qm) {
        if (StringUtils.isEmpty(qm.getName())) {
            throw new BadRequestAlertException("请填写用户姓名");
        }
        if (StringUtils.isEmpty(qm.getPhone())) {
            throw new BadRequestAlertException("请填写用户电话");
        }
        Optional<MemberReceivingInfo> memberReceivingInfo = memberReceivingInfoRepository.findByMemberCodeAndInfoCode(AppletTokenUtils.getOpenIdByToken(request).getOpenId(), qm.getInfoCode());
        if (!memberReceivingInfo.isPresent()) {
            throw new BadRequestAlertException("用户收货信息不存在");
        }
        MemberReceivingInfo receivingInfo = memberReceivingInfo.get();
        receivingInfo.setName(qm.getName());
        receivingInfo.setPhone(qm.getPhone());
        MemberReceivingInfo info = memberReceivingInfoRepository.save(receivingInfo);
        return ResponseEntity.ok(info);
    }

    @PostMapping("/applet/mine/deleteReceivingIInfo")
    @ApiOperation(value = "4-删除用户信息")
    public ResponseEntity<String> deleteReceivingInfo(@RequestParam String infoCode) {
        MemberReceivingInfo memberReceivingInfo = memberReceivingInfoRepository.findByInfoCode(infoCode);
        if (memberReceivingInfo == null) {
            throw new BadRequestAlertException("用户收货信息不存在");
        }
        memberReceivingInfoRepository.delete(memberReceivingInfo);
        return ResponseEntity.ok(memberReceivingInfo.getInfoCode());
    }
}
